<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript" src="${ctx}/js/global_util.js"></script>
<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>
<!-- jQplot CSS END -->

<h1 class="page-title">新建组织架构</h1>

<div class="container_12 clearfix leading">
    <div class="tabs side grid_6 leading">
        <section style="margin-left:0px">
            <section class="clearfix" style="display: block;">
                <div class="grid_12 alpha">
                    <div id="tree_view"></div>
                </div>
            </section>
        </section>
    </div>


    <div class="side grid_6 leading">
        <form id="post-form" class="form has-validation" action="${ctx}/organization/create.xhtml">
            <div class="clear"></div>
            <div class="clearfix">
                <label for="level_org_sel" class="form-label">机构隶属 <em>*</em></label>
                <div class="form-input">
                    <select style="width:250px" size="1" name="ClassId" id="level_org_sel" onchange="setWorkNo()">
                        <%--<option selected="" value="3">├ 业务部</option>
                        <option value="93">&#12288;&#12288;├ 业务一课</option>
                        <option value="94">&#12288;&#12288;├ 业务二课</option>
                        <option value="96">&#12288;&#12288;├ 业务三课</option>
                        <option value="97">&#12288;&#12288;├ 业务四课</option>
                        <option value="99">&#12288;&#12288;├ 业务五课</option>
                        <option value="3">├ 人事部</option>
                        <option value="3">├ 行政部</option>
                        <option value="3">├ 财务部</option>--%>
                    </select>
                </div>
            </div>

            <div class="clearfix">
                <label for="fullName" class="form-label">机构名称 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="fullName" name="organization.fullName" required="required" placeholder="请输入机构名称"/>
                </div>
            </div>

            <div class="clearfix">
                <label for="acronym" class="form-label">部门说明 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="acronym" name="organization.acronym" required="required" placeholder="请输入部门说明"/>
                </div>
            </div>

            <div class="clearfix">
                <label for="level" class="form-label">部门等级</label>
                <div class="form-input">
                    <select id="level" name="organization.level">
                        <option value="level_decision">决策系统</option>
                        <option value="level_commander">指挥系统</option>
                        <option value="level_executer">执行系统</option>
                    </select>
                </div>

                <div class="clearfix">
                    <label for="employeeNo" class="form-label">工号代码 <em>*</em></label>
                    <div class="form-input">
                        <input type="text" id="employeeNo" name="organization.employeeNo" required="required" placeholder="请输入工号代码"/>
                    </div>
                </div>

                <div class="clearfix">
                    <label for="employeeCount" class="form-label">编制人数 <em>*</em></label>

                    <div class="form-input">
                        <input type="text" id="employeeCount" name="organization.employeeCount" required="required" placeholder="请输入编制人数"/>
                    </div>
                </div>
            </div>
            <div class="form-action clearfix">
                <input type="hidden" name="organization.parent.id" id="current_organization_id"/>
                <input type="hidden" name="edit" value="${edit}"/>
                <input type="hidden" name="organization.id" id="current_id">

                <a href="javascript:void(0)" class="button" onclick="checkForm()"><span class="icon" style="background:url(${ctx}/images/img/page_edit.png)">&nbsp;</span><s:if test="#request.edit">修改</s:if><s:else>新增</else></s:else></a>
                <s:if test="#request.edit">
                <a href="javascript:void(0)" class="button" onclick="javascript:removeOrganization()"><span style="background:url(${ctx}/images/img/delete.png)" class="icon">&nbsp;</span>删除</a>
                </s:if>
                <button class="button" type="reset"><span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>重置</button>
            </div>
        </form>
    </div>
</div>

<div class="clear"></div>

<script type="text/javascript">
    var isEdit = "${edit}";
    treeview.prototype.onnodeclick = function(sender) {
        $("#current_organization_id").val(sender.id);
        getCurrentOrganization(sender.id);
        if(isEdit == "false" && isTop(sender.id)){
            findChildByLevel(sender.id);
        }
        return false;
    }
    treeview.prototype.onnodeexpand		= function(sender){
        if(isEdit == "false" && isTop(sender.id)){
            findChildByLevel(sender.id);
        }
    }
    treeview.prototype.onnodecheck = function(sender) {
    }
    treeview.prototype.onselectchange = function(before, after) {
    }

    var mTreeView = new treeview("treeview", "tree", true, false);
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();

        //getChildrenById(null);
        setTimeout("init()", 600);//0.6秒后执行init()
    });

    function init(){
        getChildrenById(null);
    }

    function getChildrenById(currentNode) {
        var paramsStr = currentNode == null ? "" : "?parentId=" + currentNode.id
        $.getJSON("${ctx}/ajax/organization-children.xhtml" + paramsStr, function(children) {
            if (children != null) {
                if (currentNode == null) {//顶层组织结构(没有父级)
                    top_ids = new Array();
                    $.each(children, function(i, child) {
                        top_ids.push(child.id);
                        if(i == 0){
                            getCurrentOrganization(child.id);
                        }
                        var n = new node(child.fullName,child.fullName, "", null,null,null,child.id);
                        mTreeView.add(n);
                        getChildrenById(n); //递归加载子结点
                    });
                } else { //非顶层
                    $.each(children, function(i, child) {
                        var n = new node(child.fullName,child.fullName, "", null,null,null,child.id);
                        currentNode.add(n);
                        getChildrenById(n); //递归加载子结点
                    });
                }

                if(currentNode == null){
                    mTreeView.create(document.getElementById("tree_view"));
                }
            }
        });
    }

    var top_ids; //保存分公司id列表
    var top_datas = new Array(); //保存分公司id和对应的子部门列表(二维数组)<topId, ArrayList<Org>>
    //判断点击的是否是分公司
    function isTop(orgId){
        if(top_ids != null && top_ids.length > 0){
            for(var i=0; i<top_ids.length; i++){
                if(top_ids[i] == orgId){
                    return true;
                }
            }
        }
        return false;
    }
    //查找分公司对应的子部门(先从缓存中找)
    function findChildByLevel(topId, defaultId, defaultFullName){
        for(var i=0; i<top_datas.length; i++){
            if(top_datas[i][0] == topId){
                getWorkNo(topId);
                fillLevelOrgSel(top_datas[i][1],defaultId, defaultFullName);
                return;
            }
        }
        $.getJSON("${ctx}/ajax/organization-top.xhtml?parentId=" + topId, function(children) {
            if (children != null) {
                getWorkNo(topId);
                top_datas.push([topId, children]);
                fillLevelOrgSel(children, defaultId, defaultFullName);
            }
        });
    }
    //填充按级别列表的组织结构
    function fillLevelOrgSel(orgs, defaultId, defaultFullName){
        if(orgs != null && orgs.length > 0){
            var temp = "<option value='0'>无</option>";
            for(var i=0; i<orgs.length; i++){
                if(defaultId > 0 && defaultId == orgs[i].id){
                    temp += "<option selected='selected' value='"+orgs[i].id+"'>"+orgs[i].fullName+"</option>"
                }else{
                    if(i == 0){
                        temp += "<option selected='selected' value='"+orgs[i].id+"'>"+orgs[i].fullName+"</option>"
                    }else{
                        temp += "<option value='"+orgs[i].id+"'>"+orgs[i].fullName+"</option>"
                    }
                }
            }
            if(defaultId >= 0){
                if(defaultFullName == undefined || defaultFullName.length == 0){
                    defaultFullName = "无";
                }
                $("#level_org_sel").prev("span").html(defaultFullName)
            }else{
                $("#level_org_sel").prev("span").html(orgs[0].fullName)
            }
            $("#level_org_sel").html(temp);

            setWorkNo();
            //$("#level_org_sel").prev("span").html(orgs[0].fullName)
        }
    }

    //设置工号代码
    function setWorkNo(){
        var id = $("#level_org_sel").val();
        if(id == 0){
            $("#employeeNo").val("");
            $("#employeeNo").attr("disabled", false);
        }else{
            $("#employeeNo").val(topWorkNo);
            $("#employeeNo").attr("disabled", true);

        }
    }
    var topWorkNo = null;
    function getWorkNo(topId){
        $.ajax({
            type: 'POST',
            dataType:"json",
            url:"${ctx}/ajax/organization-top-workno.xhtml?parentId="+topId,
            success: function(o){
                topWorkNo = o.employeeNo;
                $("#employeeNo").val(topWorkNo);
                $("#employeeNo").attr("disabled", true);
            }
        });
    }

    function checkForm(){
        if(checkNull($("#fullName").attr("value"))){
            alert("机构名称不为空");
            return false;
        }
        if(checkNull($("#acronym").attr("value"))){
            alert("部门说明不为空");
            return false;
        }
        /*
        if(checkNull($("#employeeNo").attr("value"))){
            alert("工号代码不为空");
            return false;
        }
        */
        if(checkNull($("#employeeCount").attr("value"))){
            alert("编制人数不为空");
            return false;
        }
        if(!checkInt($("#employeeCount").attr("value"))){
            alert("编制人数类型不正确");
            return false;
        }
//        $("#post-form").submit();
//        return;

        var parentId = $("#level_org_sel").val();
        $("#current_organization_id").val(parentId);

        var url = "${ctx}/organization/create.xhtml";
        if("${edit}" == "true"){
            url = "${ctx}/organization/modify.xhtml"
        }
        $.ajax(url, {
            type: "POST",
            //data: organization,
            data: $("#post-form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function() {}
        });
    }

    //得到当前组织对象(修改是得到点击的部门对象)
    function getCurrentOrganization(id){
        var isEdit = ${edit};
        if(!isEdit){
            return;
        }
        $("#current_id").val(id);
        $.getJSON("${ctx}/ajax/organization-current.xhtml?parentId=" + id, function(organization) {
            if (organization != null) {
                var topId = organization.topOrganization.id == 0 ? organization.id : organization.topOrganization.id;
                if(isTop(topId)){
                    findChildByLevel(topId, organization.parent.id, organization.parent.fullName);
                }

                $("#fullName").val(organization.fullName);
                $("#acronym").val(organization.acronym);
                $("#level").val(organization.level);
                $("#level").find("option").each(function() {
                    if(this.value == organization.level){
                        $("#uniform-level").parent().find("span:first").text(this.text);
                        return true;
                    }
                });
                $("#employeeNo").val(organization.employeeNo);
                $("#employeeCount").val(organization.employeeCount);
            }
        });
    }

    function removeOrganization(){
        if(!isEdit){
            return;
        }
        var did = $("#current_id").val();
        if(did > 0){
            if(confirm("确定要删除该组织机构吗？")){
                $.ajax({
                    type: 'POST',
                    url:"${ctx}/organization/remove.xhtml",
                    data:{"organizationId":did},
                    success: function(result){
                        if(result == true || result == "true"){
                            alert("删除成功");
                            location.reload();
                        }else{
                            alert("该组织机构不可删除");
                            return;
                        }
                    }
                });
            }
        }
    }
</script>
    
